local skel = fk.CreateSkill {
  name = "emo__fuxiang",
}

Fk:loadTranslationTable{
  ["emo__fuxiang"] = "由负向零之途",
  [":emo__fuxiang"] = "当你需要使用【杀】或【闪】时，可以发动<a href='emo__fuxiang_zhuri'>“逐日”</a>。若仍未使用则你翻至武将牌背面视为使用之。",
  -- 若你未以此法使用【杀】/【闪】，且你正面朝上，则你翻至武将牌背面视为使用【杀】/【闪】

  ["#emo__fuxiang"] = "由负向零之途：你可以发动“逐日”",
  ["#emo__fuxiang-play"] = "由负向零之途：选择1名角色，与其拼点，若你赢，使用一张拼点牌",
  ["#emo__fuxiang-jink"] = "由负向零之途：选择1名角色，与其拼点，若你赢，使用拼点牌中的【闪】",
  ["#emo__fuxiang-slash"] = "由负向零之途：选择1名角色，与其拼点，若你赢，使用拼点牌中的【杀】",
  ["emo__fuxiang_zhuri"] = "当你需要使用【杀】或【闪】时，你可以拼点：若你赢，你可以使用一张拼点牌（若不为出牌阶段空闲时间点，须为【杀】或【闪】）；若你没赢，你失去1点体力或本技能直到回合结束。",
  ["fuxiang_lose_zhuri"] = "失去“由负向零之途”",
  ["#emo__fuxiang-target"] = "请选择你使用%arg的目标",
  ["#emo__fuxiang-freeuse"] = "你可使用一张拼点牌，若不为【杀】则你翻至背面",
}

skel:addEffect("viewas", {
  pattern = "slash,jink",
  prompt = function (self, player)
    if Fk.currentResponsePattern == nil then
      return "#emo__fuxiang-play"
    elseif Exppattern:Parse(Fk.currentResponsePattern):matchExp("jink") then
      return "#emo__fuxiang-jink"
    elseif Exppattern:Parse(Fk.currentResponsePattern):matchExp("slash") then
      return "#emo__fuxiang-slash"
    end
    return "#emo__fuxiang"
  end,
  filter_pattern = {
    min_num = 1,
    max_num = 1,
    pattern = ".",
  },
  card_filter = Util.FalseFunc,
  view_as = function()
    return nil
  end,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0 and player:canPindian(to_select)
  end,
  feasible = function(self, player, selected, selected_cards)
    return #selected == 1
  end,
  on_use = function(self, room, cardUseEvent, _, params)
    local player = cardUseEvent.from
    local to = cardUseEvent.tos[1]
    local pindian = player:pindian({to}, skel.name)
    if player.dead then return skel.name end
    local cardToUse
    if pindian.results[to].winner == player then
      local ids = {}
      for _, card in ipairs({pindian.fromCard, pindian.results[to].toCard}) do
        if room:getCardArea(card) == Card.DiscardPile then
          table.insertIfNeed(ids, card:getEffectiveId())
        end
      end
      ids = table.filter(ids, function(id)
        local card = Fk:getCardById(id)
        if params == nil then -- 出牌阶段空闲时
          return player:canUse(card)
        elseif card:matchPattern(params.pattern) then
          if params.is_response then -- 打出
            return not player:prohibitResponse(card)
          elseif Exppattern:Parse(params.pattern):matchExp("jink") then -- 使用闪
            return card.trueName == "jink"
          else
            return player:canUse(card, params.extra_data)
          end
        end
      end)
      if #ids > 0 then -- 挑选可使用的真牌
        if params == nil then -- 空闲时需要特殊处理
          local use = room:askToUseRealCard(player, {
            pattern = ids, prompt = "#emo__fuxiang-freeuse", skill_name = skel.name, skip = true, expand_pile = ids,
          })
          if use then
            if use.card.trueName == "slash" then
              use.extraUse = false
              return use -- 直接返回使用此杀
            else
              room:useCard(use) -- 先使用
              if player.dead then return skel.name end
            end
          end
        else
          local cid = (#ids == 1) and ids[1] or room:askToChooseCard(player, {
            target = player, skill_name = skel.name, flag = { card_data = { { "use", ids } } }
          })
          cardToUse = Fk:getCardById(cid)
        end
      end
    else
      local choice = room:askToChoice(player, {
        choices = {"loseHp", "fuxiang_lose_zhuri"},
        skill_name = skel.name,
      })
      if choice == "loseHp" then
        room:loseHp(player, 1, skel.name)
      else -- 本回合中失去此技能
        local turn = room.logic:getCurrentEvent():findParent(GameEvent.Turn)
        if turn ~= nil and player:hasSkill(skel.name, true) then
          room:handleAddLoseSkills(player, "-"..skel.name)
          turn:addCleaner(function()
            room:handleAddLoseSkills(player, skel.name)
          end)
        end
      end
    end
    if player.dead then return skel.name end
    if not cardToUse and player.faceup then --如果未使用真牌，翻面并口嗨牌
      player:turnOver()
      if player.dead then return skel.name end
      local slash, jink = Fk:cloneCard("slash"), Fk:cloneCard("jink")
      if params == nil then -- 出牌阶段空闲时
        cardToUse = slash
      elseif params.pattern then
        if Exppattern:Parse(params.pattern):matchExp("jink") then
          cardToUse = jink
        elseif Exppattern:Parse(params.pattern):matchExp("slash") then
          cardToUse = slash
        end
      end
      cardToUse.skillName = skel.name
    end
    if cardToUse == nil then return skel.name end

    ---@type UseCardDataSpec
    local use = {
      from = player,
      tos = {},
      card = cardToUse,
      extraUse = params ~= nil, -- 若为空闲时使用，则计入次数
    }
    if not (params and params.is_response) and not cardToUse.is_passive then
      local new_use -- 重新指定目标
      if cardToUse:isVirtual() then
        new_use = room:askToUseVirtualCard(player, {
          name = cardToUse.name,
          prompt = "#emo__fuxiang-target:::"..cardToUse:toLogString(),
          skill_name = skel.name,
          extra_data = params and params.extra_data,
          cancelable = false,
          skip = true
        })
      else
        local cid = cardToUse.id
        new_use = room:askToUseRealCard(player, {
          pattern = {cid}, expand_pile = {cid}, extra_data = params and params.extra_data, cancelable = false,
          prompt = "#emo__fuxiang-target:::"..cardToUse:toLogString(), skill_name = skel.name, skip = true,
        })
      end
      if new_use then
        use.tos = new_use.tos
      else
        return skel.name
      end
    end
    return use
  end,
  enabled_at_play = function (self, player)
    return player:canUse(Fk:cloneCard("slash")) and table.find(Fk:currentRoom().alive_players, function (p)
      return player:canPindian(p)
    end)
  end,
  enabled_at_response = function (self, player, response)
    return not response and table.find(Fk:currentRoom().alive_players, function (p)
      return player:canPindian(p)
    end)
  end,
})

return skel
